package com.carb.controller.admin;

import com.carb.annotation.RequiredRoles;
import com.carb.constant.AuthorityConstant;
import com.carb.dto.EmployeeAddDTO;
import com.carb.dto.EmployeePageQueryDTO;
import com.carb.dto.EmployeeUpdateDTO;
import com.carb.result.PageResult;
import com.carb.result.Result;
import com.carb.service.IEmployeeService;
import com.carb.vo.EmployeeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工管理 前端控制器
 * </p>
 *
 * @author XuChaoyang
 * @since 2025-01-18
 */
@RestController("adminEmployeeController")
@RequestMapping("/admin/employee")
@Tag(name = "员工管理相关接口")
@Slf4j
public class EmployeeController {

    @Resource
    private IEmployeeService employeeService;

    /**
     * 新增员工
     * @param employeeAddDTO 新增员工信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PostMapping("/add")
    @Operation(summary = "新增员工")
    public Result<String> addEmployee(@RequestBody EmployeeAddDTO employeeAddDTO) {
        log.info("新增员工：{}", employeeAddDTO);
        employeeService.addEmployee(employeeAddDTO);
        log.info("新增员工成功！");
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO 查询条件
     * @return 员工分页查询结果
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @GetMapping("/page")
    @Operation(summary = "员工分页查询")
    public Result<PageResult<EmployeeVO>> pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询：{}", employeePageQueryDTO);
        PageResult<EmployeeVO> pageResult = employeeService.pageQuery(employeePageQueryDTO);
        log.info("员工分页查询成功！");
        return Result.success(pageResult);
    }

    /**
     * 根据员工id查询员工信息
     * @param id 员工id
     * @return 查询结果
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @GetMapping("/{id}")
    @Operation(summary = "根据员工id查询员工信息")
    public Result<EmployeeVO> getEmployeeById(@PathVariable @Parameter(description = "员工id") Long id) {
        log.info("根据员工id查询员工信息，员工id：{}", id);
        EmployeeVO employeeVO = employeeService.getEmployeeById(id);
        log.info("根据员工id查询员工信息成功！");
        return Result.success(employeeVO);
    }

    /**
     * 修改员工信息
     * @param employeeUpdateDTO 修改的员工信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PutMapping
    @Operation(summary = "修改员工信息")
    public Result<String> updateEmployee(@RequestBody EmployeeUpdateDTO employeeUpdateDTO) {
        log.info("修改员工信息：{}", employeeUpdateDTO);
        employeeService.updateEmployee(employeeUpdateDTO);
        log.info("修改员工信息成功！");
        return Result.success();
    }

    /**
     * 删除员工
     * @param id 员工id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @DeleteMapping("/{id}")
    @Operation(summary = "删除员工")
    public Result<String> deleteEmployee(@PathVariable @Parameter(description = "员工id") Long id) {
        log.info("删除员工，员工id：{}", id);
        employeeService.deleteEmployee(id);
        log.info("删除员工成功！");
        return Result.success();
    }
}
